// packageJ/chain-store/chainStoreSearch/chainStoreSearch.js
const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const storeGood = require('../mycomponent/storeGood')
const cartPost = require('../mycomponent/cart');
Page({
  behaviors: [skinBehavior, storeGood, cartPost],
  /**
   * 页面的初始数据
   */
  data: {
    showHistory: false, // 显示搜索历史
    storageName: "chainStore", // storage的key
    keyWork: "",
    isLoadMore: true,
    page: 1,
    total_page: 0,
    good_id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '搜索',
    })
    if (options.keyword) {
      this.setData({
        keyWork: options.keyword
      })
    }
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      })
    }
    this.setData({
      options
    })
    this.getData();
    this.getCart();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  yz_goodsPopupClose() {
    this.setData({
      good_id: '',
    })
    this.getCart();
  },
  tapGood(e) {
    let id = e.currentTarget.dataset.good.id;
    this.setData({
      good_id: id,
    })
    console.log(this.data.good_id)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 点击tag搜索
  serachTag(event) {
    let value = event.detail;
    this.setData({
      inputs: value
    });
    this.search();
  },
  hidebox(e) {
    console.log(e)
    this.setData({
      showHistory: true
    })
  },
  toSearch() {
    console.log(this.data.keyWork, this.selectComponent("#search_history"))
    if (this.selectComponent("#search_history")) {
      this.selectComponent("#search_history").appendKeywords(this.data.keyWork);
    }
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.chain-store.frontend.controller.index.search-goods");
    app._postNetWork({
      url: urlStr,
      data: {
        keyword: this.data.keyWork,
        store_id: this.data.options.store_id
      },
      success: (resdata) => {
        let res = resdata.data
        console.log(res);
        if (res.result == 1) {
          this.setData({
            info: res.data.list.data
          })
          this.data.total_page = res.data.last_page;
          this.data.isLoadMore = true;
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      urlStr = app.getNetAddresss('plugin.chain-store.frontend.controller.index.search-goods');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          keyword: this.data.keyWork,
          store_id: this.data.options.store_id

        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              info: this.data.info.concat(res.data.list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})